package com.yu.task.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu.task.globalResult.R;
import com.yu.task.globalResult.StatusType;
import com.yu.task.mapper.UserMapper;
import com.yu.task.pojo.User;
import com.yu.task.service.UserService;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private UserMapper userMapper;

    /**
     * 登录接口 1为成功
     *
     * @param user
     * @return
     */
    @Override
    public int register(User user) {
        try {
            QueryWrapper<User> qw = new QueryWrapper<User>();
            qw.eq("tel", user.getTel());
            if (userMapper.selectOne(qw) == null) {
                int i = userMapper.insert(user);
                return i;
            }
            return 2;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 登录接口 1为成功 2密码错误
     * @param user
     * @return
     */

    @Override
    public R login(User user) {
        HashMap<String,Object> map = new HashMap<>();
        try {

            QueryWrapper<User> qw = new QueryWrapper<User>();
            qw.eq("tel", user.getTel());
            User user1 = userMapper.selectOne(qw);
            if (user1 == null) {
                return R.failure(201,"账号不存在");
            } else {
                if (user1 != null && user1.getPassWord().equals(user.getPassWord())) {
                    StpUtil.login(user1.getId());
                    user1.setSaToken(StpUtil.getTokenValue());
                    user1.setPassWord(null);
                    return R.success(user1);
                }
            }
            //密码错误
            return R.failure(202,"密码错误");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e);
            return R.failure(203,"未知错误");
        }
    }

    /**
     * 更新用户信息 0为数据库异常
     * @param user
     * @return
     */

    @Override
    public int updateInfo(User user) {
        LambdaQueryWrapper lq = new LambdaQueryWrapper();
        user.setUpdateTime(new Date());
        try {
            int i = userMapper.updateById(user);
            if(i == 1){
                return 1;
            }

            return 2;
        }catch (Exception e){
            e.printStackTrace();
//            return R.failure(StatusType.SQL_ERROR, MsgType.SQL_ERROR);
            return 0;
        }

    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public int delete(Long id) {
        try {
            int i = userMapper.deleteById(id);
            if(i == 1){
                return 1;
            }
            return 2;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 分页查询用户列表
     * @return
     */
    @Override
    public R selectList(int page,int size) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        try{
            QueryWrapper<User> qw = new QueryWrapper<>();
            qw.select("id","username","gender","password","create_time","update_time","tel");
            Page<User> pageObject = new Page<>(page,size);
            Page<User> userPage = userMapper.selectPage(pageObject,qw);
            return R.success(userPage);
        }catch (Exception e){
            return R.failure();
        }

    }

    @Override
    public R getInfoById(Long uid) {
        User user = userMapper.selectById(uid);
        return R.success(user);
    }
}
